package com.sky.sms.config;

import com.aliyun.auth.credentials.Credential;
import com.aliyun.auth.credentials.provider.StaticCredentialProvider;
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient;
import com.sky.sms.core.AliyunSmsTemplate;
import com.sky.sms.properties.AliyunSmsProperties;
import darabonba.core.client.ClientOverrideConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zgp
 * @version V1.0
 * @date 2025-02-11 18:17
 */
// 启用对 AliyunSmsProperties 类的支持，使其能够从配置文件中绑定相关的配置属性，并将该类注册为一个 Spring Bean
@Configuration
@EnableConfigurationProperties(AliyunSmsProperties.class)
public class AliyunSmsAutoConfiguration {

    @Bean
    public AsyncClient asyncClient(AliyunSmsProperties aliyunSmsProperties){
        StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
                .accessKeyId(aliyunSmsProperties.getAccessKeyId())
                .accessKeySecret(aliyunSmsProperties.getAccessKeySecret())
                .build());
        AsyncClient client = AsyncClient.builder()
                .region(aliyunSmsProperties.getRegion())
                .credentialsProvider(provider)
                .overrideConfiguration(
                        ClientOverrideConfiguration.create()
                                .setEndpointOverride(aliyunSmsProperties.getEndpoint())
                ).build();
        return client;
    }

    @Bean
    public AliyunSmsTemplate aliyunSmsTemplate(AliyunSmsProperties aliyunSmsProperties, AsyncClient client){
        return new AliyunSmsTemplate(aliyunSmsProperties, client);
    }
}
